package frsf.cidisi.faia.designer;

import javax.swing.ImageIcon;

public class TableGUI extends javax.swing.JPanel {
    private ImageIcon background, type1, type2, type3;
    private boolean typeTable;
    private CellGUI [][] cells ;
    private int sizeX = 64, sizeY=48;
    private int selectedNumber;
    
    private int cellHigh = 10;
    private int cellWidth = 10;
    private int panelHigh = 800; // tamaño de panel
    private int panelWidth = 1000; // tamaño de Panel
       
    /*int windowsHigh = 2; // borde de la ventana
    int windowsWidth = 2; // borde de la ventana
    int tamaX  = 64;  // 640 /  10  = 
    int tamaY = 48;  // 480 /  10  = */ 
        
    public TableGUI() {
        initComponents();
    }

    public TableGUI(boolean tipe) {
        try {
            initComponents();
            int x,y;
            setLayout(new java.awt.GridLayout(sizeX, sizeY));
            this.typeTable = tipe;
            uploadImages();
            this.cells = new CellGUI[sizeX][sizeY];
            for (int i = 0; i < sizeX; i++){
                for (int j = 0; j < sizeY; j++){
                    cells[i][j].setType(selectedNumber);
                    cells[i][j] = new CellGUI(this); 
                    cells[i][j].setBack(background);
                    x = (i * cellHigh)+1;
                    y = (j * cellWidth)+1;
                    cells[i][j].setBounds(x, y, cellHigh, cellWidth);
                    this.add(cells[i][j]);
                }
            }
       
        }
        catch(Exception e){
            System.out.println("Initialization error. [error: frsf.cidisi.faia.designer.TableGUI-001]");
        }
    }
    
   /* public boolean getTableTipe(){
        return this.isTipeTable();
    }*/
    
    public void paint(int x, int y){
        try{
            this.cells[x][y].setType(selectedNumber);
            
            if(selectedNumber == 0) this.cells[x][y].setBack(background);
            
            if(selectedNumber == 1) this.cells[x][y].setBack(type1);
            
            if(selectedNumber == 2) this.cells[x][y].setBack(type2);
            
            if(selectedNumber == 3) this.cells[x][y].setBack(type3);
            
            //   this.casillas[x][y].setBackground(Color.red);
            this.repaint();
        }
        catch(Exception e)
        {
            System.out.println("Selection error. [error: frsf.cidisi.faia.designer.TableGUI-002]");
        }
        
        
    }
    
    private void uploadImages() {
        this.background = uploadBackground("background.gif");
        this.type1 = uploadBackground("type1.gif");
        this.type2 = uploadBackground("type2.gif");
        this.type3 = uploadBackground("type3.gif");
    }
    
    private static ImageIcon uploadBackground(String ruta) {
        java.net.URL localizacion = TableGUI.class.getResource(ruta);
        if (localizacion != null) {
            return new ImageIcon(localizacion);
        } else {
            System.err.println("File not found: " + ruta + ".  [error: frsf.cidisi.faia.designer.TableGUI-003]");
            return null;
        }
    }
    
    public int[] getCoordinates(CellGUI cell){
        try{
            int [] coordinates = new int[2]; // errorrrrr de indice... revisra
            for (int i=0; i < sizeX; i++) {
                for (int j=0; j < sizeY; j++) {
                    if (this.cells[i][j] == cell) {
                        coordinates[0] = i;
                        coordinates[1] = j;
                    }
                }
            }
                return coordinates;
        }
        catch(Exception e){
            System.out.println("Index error [error: frsf.cidisi.faia.designer.TableGUI-004]");
        }
        return null;

    }
    
    public CellGUI[][] getCells() {
        return this.cells;
    }
    
    public void setCells(CellGUI[][] cells) {
        this.cells = cells;
    }
    
    public boolean isTypeTable() {
        return typeTable;
    }    
    public void setTypeTable(boolean typeTable) {
        this.typeTable = typeTable;
    }
                              
    private void initComponents(){
        selectedNumber = 0;
        setLayout(null);
        setBackground(new java.awt.Color(0, 0, 0));
        setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        setPreferredSize(new java.awt.Dimension(panelWidth, panelHigh));
    }      
    
    public int getSelectedNumber(){
        return this.selectedNumber;
    }
    
    public void setNumeroElegido(int num){
        this.selectedNumber = num;
    }
    
    public int[][] matrix(){
      // como la matriz de 10 veces más grande que el dibujo, debo cargar la matriz a pata  
        int ret[][];
        ret = new int[sizeX*10][sizeY*10];  
        for (int i = 0; i < sizeX; i++){
            for (int j = 0; j < sizeY; j++){
                for(int t = 0; t < 10; t++){
                    for(int o = 0; o < 10; o++){
                        ret[(i*10)+t][(j*10)+o] = this.cells[i][j].getType();
                    }
                }
            }
        }
        return ret;
    }
}   

    